<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="案例标题" prop="caseTitle">
        <el-input
          v-model="queryParams.caseTitle"
          placeholder="请输入案例标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="案件类别" prop="caseCategory">
        <el-select v-model="queryParams.caseCategory" placeholder="请选择案件类别" clearable>
          <el-option
            v-for="dict in dict.type.case_category"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:case:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:case:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:case:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:case:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="caseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="案例" align="center" prop="caseId"/>
      <el-table-column label="案例标题" align="center" prop="caseTitle"/>
      <el-table-column label="案例内容" align="center" prop="caseContent">
        <template slot-scope="scope">
          <div class="ellipsis" @click="handleView(scope.row)">
            <!--            {{ scope.row.newsContent | truncate(5) }} &lt;!&ndash; 显示前5个字 &ndash;&gt;-->
            {{ scope.row.caseContent.slice(0, 3) + '...' }} <!-- 显示前3字 -->
          </div>
        </template>

      </el-table-column>
      <el-table-column label="封面图" align="center" prop="coverImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.coverImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="案件类别" align="center" prop="caseCategory">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.case_category" :value="scope.row.caseCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="浏览次数" align="center" prop="viewCount"/>
      <el-table-column label="状态" align="center" prop="caseStatus">

        <template slot-scope="scope">
          <dict-tag :options="dict.type.case_status" :value="scope.row.caseStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:case:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:case:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增详情对话框 -->
    <el-dialog :title="viewTitle" :visible.sync="viewOpen" width="800px" class="el-dialog_body" append-to-body>
      <div class="case-detail">
        <h2>{{ viewForm.caseTitle }}</h2>
        <div class="detail-meta">
          <dict-tag :options="dict.type.case_category" :value="viewForm.caseCategory"/>
          <span>浏览次数：{{ viewForm.viewCount }}</span>
        </div>
        <div v-html="viewForm.caseContent" class="case-content"></div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewOpen = false">关闭</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改经典案例对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="案例标题" prop="caseTitle">
          <el-input v-model="form.caseTitle" placeholder="请输入案例标题"/>
        </el-form-item>
        <el-form-item label="案例内容">
          <editor v-model="form.caseContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="封面图URL" prop="coverImage">
          <image-upload v-model="form.coverImage"/>
        </el-form-item>
        <el-form-item label="案件类别" prop="caseCategory">
          <el-radio-group v-model="form.caseCategory">
            <el-radio
              v-for="dict in dict.type.case_category"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="浏览次数" prop="viewCount">
          <el-input v-model="form.viewCount" placeholder="请输入浏览次数"/>
        </el-form-item>
        <el-form-item label="状态" prop="caseStatus">
          <el-radio-group v-model="form.caseStatus">
            <el-radio
              v-for="dict in dict.type.case_status"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否删除" prop="isDeleted">
          <el-radio-group v-model="form.isDeleted">
            <el-radio
              v-for="dict in dict.type.is_del"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listCase, getCase, delCase, addCase, updateCase} from "@/api/lawyer_aid/case"

export default {
  name: "Case",
  dicts: ['case_category', 'case_status', 'is_deleted'],
  data() {
    return {
      //详情表单
      viewOpen: false,
      viewTitle: '案列详情',
      viewForm: {},

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 经典案例表格数据
      caseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        caseTitle: null,
        caseCategory: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        caseTitle: [
          {required: true, message: "案例标题不能为空", trigger: "blur"}
        ],
        caseContent: [
          {required: true, message: "案例内容不能为空", trigger: "blur"}
        ],
        caseCategory: [
          {required: true, message: "案件类别不能为空", trigger: "change"}
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 查看案列详情方法
    handleView(row) {
      this.viewForm = {...row} // 直接使用表格行数据，如需优化可单独请求详情接口
      this.viewOpen = true
    },

    /** 查询经典案例列表 */
    getList() {
      this.loading = true
      listCase(this.queryParams).then(response => {
        this.caseList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        caseId: null,
        caseTitle: null,
        caseContent: null,
        coverImage: null,
        caseCategory: null,
        viewCount: null,
        caseStatus: null,
        isDeleted: null,
        createTime: null,
        createdBy: null,
        updatedBy: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.caseId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加经典案例"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const caseId = row.caseId || this.ids
      getCase(caseId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改经典案例"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.caseId != null) {
            updateCase(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addCase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const caseIds = row.caseId || this.ids
      this.$modal.confirm('是否确认删除经典案例编号为"' + caseIds + '"的数据项？').then(function () {
        return delCase(caseIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/case/export', {
        ...this.queryParams
      }, `case_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style scoped>

.el-dialog_body {
  max-height: 600px;
  overflow-y: auto;
}

.case-detail {
  padding: 20px;
}

.case-detail h2 {
  font-size: 1.5em;
  margin-bottom: 15px;
  color: #333;
}

.detail-meta {
  color: #666; /* 原有颜色 */
  margin-bottom: 20px; /* 元数据与新闻内容的间距（原有逻辑） */
}

.detail-meta span {
  margin-right: 20px; /* 每个 span 右侧间距 */
  white-space: nowrap; /* 防止内容换行 */
}

.case-content {
  line-height: 1.8;
  color: #444;
}
</style>
